//
//  StatusItem.m
//  KotnetAutologin
//
//	handle status item
//
//  Created by Kristof Marien on 31/12/09.
//  Copyright 2009 KotnetAutologin. All rights reserved.
//

#import "StatusItem.h"


@implementation StatusItem
/**
 *  create the status item
 */
- (void) create:(NSMenu *)menu
{
	statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
	
	NSBundle *bundle = [NSBundle mainBundle];
	
	statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon" ofType:@"png"]];
	
	statusMenu = menu;
	[statusItem setImage:statusImage];
	[statusItem setMenu:statusMenu];
	[statusItem setToolTip:@"Kotnet Autologin"];
	[statusItem setHighlightMode:YES];
}
/**
 *  change the tooltip
 */
- (void) changeTooltip:(NSString *)tooltip
{
	[statusItem setToolTip:[NSString stringWithFormat:@"%@%@", @"Kotnet Autologin - ", tooltip]];
}
/**
 *  change the title of an item
 */
- (void) changeItem:(NSInteger)tag :(NSString *)title
{
	[[statusMenu itemWithTag:tag] setTitle:title];
	[[statusMenu itemWithTag:2] setAction:@selector(clicked)];
}
/**
 *  change the icon
 */
- (void) changeIcon:(NSString *)icon
{
	NSBundle *bundle = [NSBundle mainBundle];
	if (icon == @"error") {
		statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon_error" ofType:@"png"]];
	} else if (icon == @"succeeded") {
		statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon_succeeded" ofType:@"png"]];
	} else {
		statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon" ofType:@"png"]];
	}
	[statusItem setImage:statusImage];
}
/**
 *  set an action to an item
 */
- (void) setAction:(NSInteger)tag :(SEL)action
{
	[[statusMenu itemWithTag:tag] setAction:action];
}
/**
 *  remove status item
 */
- (void) dealloc
{
	[statusImage release];
	[super dealloc];
}
@end
